package com.weed.pattern.strategy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 简单策略门面实现
 *
 * @author zhong.wang
 * @date 2024/10/22
 */
public class GenricStrategyManager<S extends Strategy> implements StrategyManager<S> {
    /**
     * 策略容器
     */
    private final Map<String, S> strategyRegister = new ConcurrentHashMap<>();

    /**
     * 新增策略
     *
     * @param strategy 策略
     */
    @Override
    public void addStrategy(S strategy) {
        addStrategy(strategy.getId(),strategy);
    }

    /**
     * 新增策略
     *
     * @param id
     * @param strategy
     */
    @Override
    public void addStrategy(String id, S strategy) {
        strategyRegister.put(id,strategy);
    }

    /**
     * 根据策略id 获取策略
     *
     * @param provider 策略id
     * @return
     */
    @Override
    public final S getStrategy(String provider) {
        return strategyRegister.getOrDefault(provider,null);
    }

    /**
     * 是否包含策略
     * @param provider
     * @return
     */
    @Override
    public final boolean containStrategy(String provider){
        return strategyRegister.containsKey(provider);
    }

    /**
     * 获取所有策略
     *
     * @return
     */
    @Override
    public final List<S> getStrategies() {
        return new ArrayList<>(strategyRegister.values());
    }

    /**
     * 获取策略容器
     * @return
     */
    public Map<String, S> getStrategyRegister() {
        return strategyRegister;
    }
}
